package cn.yuanqiao.web.controller.archive.storeRoom;

import cn.yuanqiao.archive.storeRoom.domain.Box;
import cn.yuanqiao.archive.storeRoom.domain.UpDownShelves;
import cn.yuanqiao.archive.storeRoom.service.IBoxService;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 盒-格子Controller
 *
 * @author xy
 * @date 2023-06-26
 */
@RestController
@RequestMapping("/archive/box")
public class BoxController extends BaseController
{
    @Autowired
    private IBoxService boxService;

    /**
     * 查询盒-格子列表
     */
    @PreAuthorize("@ss.hasPermi('storeroom:box:list')")
    @GetMapping("/list")
    public TableDataInfo list(Box box)
    {
        startPage();
        List<Box> list = boxService.selectBoxList(box);
        return getDataTable(list);
    }

    /**
     * 导出盒-格子列表
     */
    @PreAuthorize("@ss.hasPermi('storeroom:box:export')")
    @Log(title = "盒-格子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Box box)
    {
        List<Box> list = boxService.selectBoxList(box);
        ExcelUtil<Box> util = new ExcelUtil<Box>(Box.class);
        util.exportExcel(response, list, "盒-格子数据");
    }

    /**
     * 获取盒-格子详细信息
     */
    @PreAuthorize("@ss.hasPermi('storeroom:box:query')")
    @GetMapping(value = "/{boxId}")
    public AjaxResult getInfo(@PathVariable("boxId") Long boxId)
    {
        return success(boxService.selectBoxByBoxId(boxId));
    }

    /**
     * 新增盒-格子
     */
    @PreAuthorize("@ss.hasPermi('storeroom:box:add')")
    @Log(title = "盒-格子", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Box box)
    {
        return toAjax(boxService.insertBox(box));
    }

    /**
     * 修改盒-格子
     */
    @PreAuthorize("@ss.hasPermi('storeroom:box:edit')")
    @Log(title = "盒-格子", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Box box)
    {
        return toAjax(boxService.updateBox(box));
    }

    /**
     * 删除盒-格子
     */
    @PreAuthorize("@ss.hasPermi('storeroom:box:remove')")
    @Log(title = "盒-格子", businessType = BusinessType.DELETE)
	@DeleteMapping("/{boxIds}")
    public AjaxResult remove(@PathVariable Long[] boxIds)
    {
        return toAjax(boxService.deleteBoxByBoxIds(boxIds));
    }

    @PostMapping("/isPermit")
    public AjaxResult isPermit(@RequestBody  Map map){
        Map result=boxService.isPermit(map);
        boolean isPermit= (boolean) result.get("isPermit");
        if(isPermit){
            return AjaxResult.success(result);
        }else{
            return AjaxResult.error(result.get("msg").toString());
        }
    }

    @PostMapping("/getMaxHehaoByCondition")
    public AjaxResult getMaxHehaoByCondition(@RequestBody  Map map){
        Map result=boxService.getMaxHehaoByCondition(map);
        if((boolean)result.get("flag")) {
        return AjaxResult.success(result);
        }else{
            return AjaxResult.error(result.get("message")==null?"无法获取盒号":result.get("message").toString());
        }
    }
    @Log(title = "装盒", businessType = BusinessType.INSERT)
    @PostMapping("/saveBox")
    @PreAuthorize("@ss.hasPermi('archive:box:saveBox')")
    public AjaxResult saveBox(@RequestBody  Map map){
        Map result=boxService.saveBox(map);
       if((boolean)result.get("flag")) {
           return AjaxResult.success(result);
       }else{
           return AjaxResult.error(result.get("message")==null?"装盒失败":result.get("message").toString());
       }
    }
    @PostMapping("/updateBoxNumber")
    public AjaxResult updateBoxNumber(@RequestBody  Map map){
        Map result=boxService.updateBoxNumber(map);
        if((boolean)result.get("result")){
            return AjaxResult.success(result);
        }else{
            return AjaxResult.error(result.get("message")==null?"排号失败！":result.get("message").toString());

        }

    }
    @Log(title = "取消装盒", businessType = BusinessType.DELETE)
    @PostMapping("/cancelBox")
    @PreAuthorize("@ss.hasPermi('archive:box:cancelBox')")
    public AjaxResult cancelBox(@RequestBody  Map map){
        Map result=boxService.cancelBox(map);
        if((boolean)result.get("isPermit")){
            return AjaxResult.success(result);
        }else{
            return AjaxResult.error(result.get("message")==null?"取消装盒文件！":result.get("message").toString());
        }

    }

    @GetMapping("/getFlag")
    public AjaxResult getFlag(@RequestParam("daIds") String daIds,@RequestParam("type")String type){
        Boolean result=boxService.getFlag(daIds,type);
        return AjaxResult.success(result);

    }



    @Log(title = "档案盒记录", businessType = BusinessType.SELECT)
    @GetMapping("/boxList")
    public TableDataInfo boxList(Box box)
    {
        startPage();
        List<UpDownShelves> list = boxService.selectList(box);
        return getDataTable(list);
    }


}
